home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / LOCK.C < prev    next >
C/C++ Source or Header  |  1993-04-10  |  9KB  |  261 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    l o c k . c                                                     */
  3. /*                                                                    */
  4. /*    Locking functions for UUPC/extended                             */
  5. /*                                                                    */
  6. /*    Copyright (c) 1992 by Kendra Electronic Wonderworks; all        */
  7. /*    rights reserved except those explicitly granted by the          */
  8. /*    UUPC/extended license.                                          */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. /*--------------------------------------------------------------------*/
  12. /*                          RCS Information                           */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*
  16.  *    $Id: LOCK.C 1.8 1993/04/11 00:31:04 ahd Exp $
  17.  *
  18.  *    Revision history:
  19.  *    $Log: LOCK.C $
  20.  *     Revision 1.8  1993/04/11  00:31:04  ahd
  21.  *     Global edits for year, TEXT, etc.
  22.  *
  23.  *     Revision 1.7  1993/03/24  01:57:30  ahd
  24.  *     Add string.h include
  25.  *
  26.  *     Revision 1.6  1993/03/06  22:48:23  ahd
  27.  *     Allow nested locks
  28.  *
  29.  * Revision 1.5  1992/11/28  19:51:16  ahd
  30.  * Issue lock based on lock file existence to avoid FOPEN retry loop
  31.  *
  32.  * Revision 1.4  1992/11/19  02:58:54  ahd
  33.  * drop rcsid
  34.  *
  35.  * Revision 1.3  1992/11/19  02:54:47  ahd
  36.  * Revision 1.2  1992/11/17  13:47:30  ahd
  37.  * Do not buffer lock file
  38.  *
  39.  * Revision 1.1  1992/11/16  05:00:26  ahd
  40.  * Initial revision
  41.  *
  42.  */
  43.  
  44.  
  45. /*--------------------------------------------------------------------*/
  46. /*                   Standard library include files                   */
  47. /*--------------------------------------------------------------------*/
  48.  
  49. #include <stdio.h>
  50. #include <time.h>
  51. #include <sys/types.h>
  52. #include <process.h>
  53. #include <string.h>
  54. #include <io.h>
  55.  
  56. /*--------------------------------------------------------------------*/
  57. /*                    UUPC/extended include files                     */
  58. /*--------------------------------------------------------------------*/
  59.  
  60. #include "lib.h"
  61. #include "dater.h"
  62. #include "stater.h"
  63. #include "lock.h"
  64. #include "hlib.h"
  65. #include "import.h"
  66.  
  67. /*--------------------------------------------------------------------*/
  68. /*                      Define current file name                      */
  69. /*--------------------------------------------------------------------*/
  70.  
  71. currentfile();
  72.  
  73. /*--------------------------------------------------------------------*/
  74. /*                          Local variables                           */
  75. /*--------------------------------------------------------------------*/
  76.  
  77. static FILE *locket = NULL;
  78. static char lname[FILENAME_MAX];
  79.  
  80. boolean locked = FALSE;
  81.  
  82. /*--------------------------------------------------------------------*/
  83. /*    L o c k S y s t e m                                             */
  84. /*                                                                    */
  85. /*    Get the lock for a system                                       */
  86. /*--------------------------------------------------------------------*/
  87.  
  88. boolean LockSystem( const char *system , long program )
  89. {
  90.    time_t age;
  91.    char fname[FILENAME_MAX];
  92.    char *extension;
  93.  
  94. /*--------------------------------------------------------------------*/
  95. /*                Don't lock unless in multitask mode                 */
  96. /*--------------------------------------------------------------------*/
  97.  
  98.    if ( ! bflag[ F_MULTITASK ] )
  99.       return TRUE;
  100.  
  101. /*--------------------------------------------------------------------*/
  102. /*                Verify our lock is not already in use               */
  103. /*--------------------------------------------------------------------*/
  104.  
  105.    if ( locket != NULL )
  106.    {
  107.       printmsg(0,
  108.          "LockSystem: Attempt to lock %s with lock file %s already open",
  109.          system,
  110.          lname );
  111.       panic();
  112.    } /* if */
  113.  
  114. /*--------------------------------------------------------------------*/
  115. /*                        Try to get the lock                         */
  116. /*--------------------------------------------------------------------*/
  117.  
  118.    switch( program )
  119.    {
  120.       case B_NEWS:
  121.          extension = "LCN";
  122.          break;
  123.  
  124.       case B_UUSTAT:
  125.          extension = "LCS";
  126.          break;
  127.  
  128.       case B_UUCICO:
  129.          extension = "LCK";
  130.          break;
  131.  
  132.       case B_UUXQT:
  133.          extension = "LCX";
  134.          break;
  135.  
  136.       default:
  137.          extension = "LCZ";
  138.          break;
  139.    } /* switch */
  140.  
  141.    sprintf( fname,
  142.             "%s/locks.lck/%.8s.%s",
  143.             E_spooldir,
  144.             system,
  145.             extension );
  146.  
  147.    importpath( lname, fname, system );
  148.  
  149. /*--------------------------------------------------------------------*/
  150. /*    Determine if the lock exists, and unlink it if so.  If this     */
  151. /*    fails, we can't get the lock, so we return gracefully.          */
  152. /*    (This allows to bypass the "helpful" FOPEN retries of the       */
  153. /*    failed open.                                                    */
  154. /*--------------------------------------------------------------------*/
  155.  
  156.    if ( access( lname, 0 ) || !unlink( lname ))
  157.       locket = FOPEN( lname, "w",TEXT_MODE );
  158.  
  159.    if ( locket == NULL )
  160.    {
  161.       long size;
  162.  
  163.       age = stater( lname, &size );
  164.  
  165.       printmsg(1, "System %s already locked since %s",
  166.                system,
  167.                (size > 0) ? dater( age, NULL ) : "UNKNOWN" );
  168.       return FALSE;
  169.    }
  170.  
  171.    setvbuf( locket, NULL, _IONBF, 0);
  172.  
  173.    time( &age );
  174.    fprintf( locket, "%s locked by process %ld at %s",
  175.                     system, (long) getpid(), ctime( &age ));
  176.    fflush( locket );          /* Force the file to exist on disk  */
  177.  
  178. /*--------------------------------------------------------------------*/
  179. /*     We don't close the file, since the open file *is* the lock     */
  180. /*--------------------------------------------------------------------*/
  181.  
  182.    locked = TRUE;
  183.    return TRUE;
  184.  
  185. } /* LockSystem */
  186.  
  187. /*--------------------------------------------------------------------*/
  188. /*    U n l o c k S y s t e m                                         */
  189. /*                                                                    */
  190. /*    Release a lock created by LockSystem                            */
  191. /*--------------------------------------------------------------------*/
  192.  
  193. void UnlockSystem( void )
  194. {
  195.  
  196. /*--------------------------------------------------------------------*/
  197. /*                Don't lock unless in multitask mode                 */
  198. /*--------------------------------------------------------------------*/
  199.  
  200.    if ( ! bflag[ F_MULTITASK ] )
  201.       return;
  202.  
  203. /*--------------------------------------------------------------------*/
  204. /*                       Verify we hold a lock                        */
  205. /*--------------------------------------------------------------------*/
  206.  
  207.    if ( locket == NULL )
  208.    {
  209.       printmsg(0,"UnlockSystem: No lock held");
  210.       panic();
  211.    } /* if */
  212.  
  213. /*--------------------------------------------------------------------*/
  214. /*                          Release the lock                          */
  215. /*--------------------------------------------------------------------*/
  216.  
  217.    fclose( locket );
  218.    locket = NULL;
  219.    locked = FALSE;
  220.  
  221.    unlink( lname );
  222.  
  223. } /* UnlockSystem */
  224.  
  225. /*--------------------------------------------------------------------*/
  226. /*       P u s h L o c k                                              */
  227. /*                                                                    */
  228. /*       Save lock status in order to perform second lock             */
  229. /*--------------------------------------------------------------------*/
  230.  
  231.  
  232. void PushLock( LOCKSTACK *top )
  233. {
  234.    top->locket = locket;
  235.    if ( locket !=  NULL )
  236.    {
  237.       strcpy( top->lname, lname );
  238.       locket =  NULL;
  239.    }
  240.    locked = FALSE;
  241.  
  242. }
  243.  
  244. /*--------------------------------------------------------------------*/
  245. /*       P o p L o c k                                                */
  246. /*                                                                    */
  247. /*       Restore previous lock information                            */
  248. /*--------------------------------------------------------------------*/
  249.  
  250. void PopLock( LOCKSTACK *top )
  251. {
  252.    locket = top->locket;
  253.  
  254.    if ( locket !=  NULL )
  255.    {
  256.       strcpy( lname, top->lname );
  257.       locked = TRUE;
  258.    }
  259.  
  260. } /* PopLock */
  261.